package models

import (
	"github.com/astaxie/beego/orm"
)

type Nav struct {
	Id        int    `orm:"column(id);pk;auto"`
	Name      string `orm:"column(name);size(50)"`
	Url       string `orm:"column(url);size(255)"`
	Icon      string `orm:"column(icon);size(500);null"`
	Target    string `orm:"column(target);size(20);default(_self)"`
	Sort      int    `orm:"column(sort);default(0)"`
	Status    int8   `orm:"column(status);default(0)"`
	Color     string `orm:"column(color);size(20);null"`
	CreatedAt string `orm:"column(created_at);type(datetime);auto_now_add"`
	UpdatedAt string `orm:"column(updated_at);type(datetime);auto_now"`
}

func (n *Nav) TableName() string {
	return "navs"
}

func init() {
	orm.RegisterModel(new(Nav))
}

// GetActiveNavs 获取所有启用的导航
func (n *Nav) GetActiveNavs() ([]*Nav, error) {
	var navs []*Nav
	_, err := orm.NewOrm().QueryTable(n.TableName()).
		Filter("status", 0).
		OrderBy("sort").
		All(&navs)
	return navs, err
}

// GetAllNavs 获取所有导航（包括启用和禁用的）
func (n *Nav) GetAllNavs() ([]*Nav, error) {
	var navs []*Nav
	_, err := orm.NewOrm().QueryTable(n.TableName()).
		OrderBy("sort").
		All(&navs)
	return navs, err
}

// NewNav 创建一个新的导航实例
func NewNav() *Nav {
	return &Nav{}
}

// GetCurrentNav 获取当前导航
func (n *Nav) GetCurrentNav(path string) *Nav {
	var nav Nav
	err := orm.NewOrm().QueryTable(n).Filter("url", path).Filter("status", 0).One(&nav)
	if err != nil {
		return nil
	}
	return &nav
}
